//
//  JavaScriptHelper.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2017/2/14.
//  Copyright © 2021年 梁慧聪. All rights reserved.
//

#import "CLJavaScriptHelper.h"
#import "CLWebDataView.h"
#import "CLUserDefaults.h"
@interface CLJavaScriptHelper ()

@property (nonatomic, strong) NSCache *jsCache;

@end

@implementation CLJavaScriptHelper

SYNTHESIZE_SINGLETON_FOR_CLASS(CLJavaScriptHelper)

- (NSCache *)jsCache{
    if (!_jsCache) {
        _jsCache = [[NSCache alloc] init];
    }
    return _jsCache;
}

- (NSString *)getJSSourceWithName:(NSString *)name{
    NSCParameterAssert(name);
    
    NSString *source;
    if ((source = [self.jsCache objectForKey:name])) {
        return source;
    }
    
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"js"];
    source = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
    NSCParameterAssert(source);
    [self.jsCache setObject:source forKey:name];
    return source;
}

+ (NSString *)getJSSourceWithName:(NSString *)name{
    return [[self sharedInstance] getJSSourceWithName:name];
}

+ (void)loadJavascriptWithName:(NSString *)name webView:(CLWebView *)webView{
    if (![webView.mainFURL hasPrefix:@"http"]) {
        return;
    }
    NSString *source = [self getJSSourceWithName:name];
    if (source) {
        [webView evaluateJavaScript:source completionHandler:^(NSString *msg, NSError *error) {
            if (error) {
                NSString *string = error.userInfo.description;
                if (!NULLString(string)) {
                    string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
                    string = [string stringByReplacingOccurrencesOfString:@"\r" withString:@""];
                    string = [string stringByReplacingOccurrencesOfString:@"    " withString:@""];
                }
                NSLog(@"[js][error][%@]:%@",name,string);
            } else {
                [CLJavaScriptHelper loadFinish:webView.request.URL.absoluteString withName:name];
            }
        }];
    }
}

+ (void)setNoImageMode:(BOOL)enabled webView:(CLWebView *)webView loadPrimaryScript:(BOOL)needsLoad{
    if (needsLoad) {
        [self loadJavascriptWithName:@"NoImageModeHelper" webView:webView];
    }
    
    [webView evaluateJavaScript:[NSString stringWithFormat:@"window.cl.NoImageMode.setEnabled(%d)",enabled] completionHandler:nil];
}
    
+ (void)setLongPressGestureWithWebView:(CLWebView *)webView{
    [self loadJavascriptWithName:@"ContextMenu" webView:webView];
}

+ (void)setFindInPageWithWebView:(CLWebView *)webView{
    [self loadJavascriptWithName:@"FindInPage" webView:webView];
}

+ (void)setBaiduADBlockWithWebView:(CLWebView *)webView{
    [self loadJavascriptWithName:@"BaiduADBlock" webView:webView];
}

+ (void)setEyeProtectiveWithWebView:(CLWebView *)webView colorValue:(NSString *)colorValue loadPrimaryScript:(BOOL)needsLoad{
    if (needsLoad) {
        [self loadJavascriptWithName:@"EyeProtective" webView:webView];
    }
    NSLog(@"[EYE] 护眼模式设置：%@",colorValue);
    
    [webView.bridge callHandler:@"setThemeStyle" data:@{@"colorValue":colorValue} responseCallback:^(NSString *responseData) {
        NSLog(@"[setThemeStyle],%@",responseData);
    }];
    
//    [webView evaluateJavaScript:[NSString stringWithFormat:@"window.cl.EyeProtective.setColorValue(%@)",colorValue] completionHandler:nil];
}

+ (void)setEyeProtectiveWithWebView:(CLWebView *)webView on:(NSInteger)isOn loadPrimaryScript:(BOOL)needsLoad {
    if (needsLoad) {
        [self loadJavascriptWithName:@"Darkmode" webView:webView];
    }
    
    [webView evaluateJavaScript:@"new Darkmode().showWidget()" completionHandler:^(NSString *msg, NSError *error) {
        
    }];
}

+ (void)setTestJSBlockWithWebView:(CLWebView *)webView {
    [self loadJavascriptWithName:@"TestJS" webView:webView];
}

+ (void)loadFinish:(NSString *)url withName:(NSString *)name {
    if (url) {
        [CLJavaScriptHelper.sharedInstance.jsLoadRecord setObject:@(YES) forKey:[NSString stringWithFormat:@"%@-%@",name,url]];
//        NSLog(@"[%@] loaded",name);
    }
}

+ (BOOL)boolLoadFinish:(NSString *)url withName:(NSString *)name {
    if (!NULLString(url)) {
        return [[CLJavaScriptHelper.sharedInstance.jsLoadRecord objectForKey:[NSString stringWithFormat:@"%@-%@",name,url]] boolValue];
    }
    return NO;
}

- (NSDictionary *)xpathInfo {
    if (!_xpathInfo) {
        NSURL *URL=[[NSBundle mainBundle]URLForResource:@"CLXpath" withExtension:@"plist"];
        _xpathInfo=[NSDictionary dictionaryWithContentsOfURL:URL];
    }
    return _xpathInfo;
}

- (NSMutableDictionary *)jsLoadRecord {
    if (!_jsLoadRecord) {
        _jsLoadRecord = [NSMutableDictionary dictionary];
    }
    return _jsLoadRecord;
}
@end
